﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Avatar.Model;
using AvatarHelper;

namespace Avatar.Company
{
    public static class CompanyInformationManager
    {
        public static void SaveCompanyInformation(
            string _user,
            string _companyName,
            int _size,
            int _nature,
            int _industry,
            int _location,
            DateTime _dateOfEstablishment,
            string _description,
            string _registeredCapital
            )
        {
            try
            {
                #region 入口检测

                if (string.IsNullOrEmpty(_companyName)) throw new Exception("企业名称不能为空");
                if (string.IsNullOrEmpty(_description)) throw new Exception("企业简介不能为空");
                if (!string.IsNullOrEmpty(_companyName) && _companyName.Length > 50) throw new Exception("企业名称50个字符");
                if (!string.IsNullOrEmpty(_description) && _description.Length > 3000) throw new Exception("企业简介内容不能超过3000个字符");
                if (!string.IsNullOrEmpty(_registeredCapital))
                {
                    if (_registeredCapital.Length > 5) throw new Exception("注册资金不能超过5位");
                    try
                    {
                        Convert.ToInt32(_registeredCapital);
                    }
                    catch (Exception)
                    {
                        throw new Exception("注册资金填写错误，必须填写数字。");
                    }
                }
                #endregion

                using (var _adc = DBHelper.GetDataContext())
                {
                    var _b = _adc.CompanyInformation.Where(i => i.User == _user).FirstOrDefault();
                    if (_b == null)
                    {
                        _b = new CompanyInformation();

                        _b.User = _user;
                        _b.CompanyName = _companyName;
                        _b.Size = _size;
                        _b.Nature = _nature;
                        _b.Industry = _industry;
                        _b.Location = _location;
                        _b.DateOfEstablishment = _dateOfEstablishment;
                        _b.Description = _description;
                        _b.RegisteredCapital = _registeredCapital;

                        _adc.CompanyInformation.InsertOnSubmit(_b);
                    }
                    else
                    {
                        _b.CompanyName = _companyName;
                        _b.Size = _size;
                        _b.Nature = _nature;
                        _b.Industry = _industry;
                        _b.Location = _location;
                        _b.DateOfEstablishment = _dateOfEstablishment;
                        _b.Description = _description;
                        _b.RegisteredCapital = _registeredCapital;
                    }
                    _adc.SubmitChanges();
                }
            }
            catch (Exception _e)
            {
                throw new Exception("保存企业基本信息出错：" + _e.Message);
            }
        }
        /// <summary>
        /// 根据'企业用户的用户名'获取企业信息
        /// </summary>
        /// <param name="_user"></param>
        /// <returns></returns>
        public static CompanyInformation GetCompanyInformation(string _user)
        {
            return DBHelper.Get<CompanyInformation>(b => b.User == _user, "获取企业基本资料出错：");
        }
        /// <summary>
        /// 根据'企业名称'获取企业信息
        /// </summary>
        /// <param name="_companyName"></param>
        /// <returns></returns>
        public static CompanyInformation GetCompanyInformationByCompanyName(string _companyName)
        {
            return DBHelper.Get<CompanyInformation>(b => b.CompanyName == _companyName, "获取企业基本资料出错：");
        }
        /// <summary>
        /// 删除企业资料
        /// </summary>
        /// <param name="_user"></param>
        /// <returns></returns>
        public static bool Delete(string _user)
        {
            //var _i = GetCompanyInformation(_user);
            //return DBHelper.Delete(_i,"删除企业基本资料出错：");
            throw new NotImplementedException();
        }
        /// <summary>
        /// 统计系统收录企业信息总数
        /// </summary>
        /// <returns></returns>
        public static int CompanyCount()
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                return _adc.CompanyInformation.Count();
            }
        }
    }
}
